package com.puman.watchtrade.fragment.personal.adapter;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pingplusplus.android.PaymentActivity;
import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.imageLoader.util.UniversalImageLoadTool;
import com.puman.watchtrade.fragment.info.model.InfoDatas;
import com.puman.watchtrade.fragment.personal.BiddingRecordFregment;
import com.puman.watchtrade.fragment.personal.model.BiddRecords;
import com.puman.watchtrade.util.ImageDownloader;
import com.puman.watchtrade.util.OnImageDownload;

public class BiddingRecordListViewAdapter extends BaseAdapter {

	private LayoutInflater listContainer; // ��ͼ����
	private View view;
	private Activity activity;
	private ImageDownloader mDownloader;
	private BiddRecords biddRecords = new BiddRecords();
	private OnClickListener onClickListener;
	private BiddingRecordFregment mContext;

	public final class ListItemView {
		public TextView title;
		public TextView price;
		public TextView datetime;
		public TextView toPay;
		public ImageView image;
		public ImageView deal;
	}

	public BiddingRecordListViewAdapter(Activity activity,
			BiddRecords biddRecords, OnClickListener onClickListener,
			BiddingRecordFregment mContext) {
		this.activity = activity;
		this.biddRecords = biddRecords;
		this.onClickListener = onClickListener;
		this.mContext = mContext;
		listContainer = LayoutInflater.from(activity);
		// TODO Auto-generated constructor stub
	}

	/**
	 */
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ListItemView listItemView;
		final int selectID = position;
		if (convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.bidding_record_item,
					null);
			listItemView.title = (TextView) convertView
					.findViewById(R.id.info_title);
			listItemView.price = (TextView) convertView
					.findViewById(R.id.price_input);
			listItemView.datetime = (TextView) convertView
					.findViewById(R.id.datetime);
			listItemView.image = (ImageView) convertView
					.findViewById(R.id.info_item_image);
			listItemView.toPay = (TextView) convertView
					.findViewById(R.id.to_pay);
			listItemView.deal = (ImageView) convertView.findViewById(R.id.info_item_image_mark);
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();

		}
		if (biddRecords.biddRecordList.get(position).getTitle().length() > 20) {
			listItemView.title.setText(biddRecords.biddRecordList.get(position)
					.getTitle().substring(0, 20)
					+ "...");
		} else {
			listItemView.title.setText(biddRecords.biddRecordList.get(position)
					.getTitle());
		}
		listItemView.datetime.setText(biddRecords.biddRecordList.get(position)
				.getDatetime());
		String price = "￥"
				+ biddRecords.biddRecordList.get(position).getFinalPrice()
				+ "/ ￥" + biddRecords.biddRecordList.get(position).getMyPrice();
		listItemView.price.setText(price);

		String urlString = biddRecords.biddRecordList.get(position)
				.getImageUrl().replace(" ", "");
		if (!urlString.equals("")) {
			UniversalImageLoadTool.display(urlString,
					(ImageView) listItemView.image, R.drawable.load_default);
		}

		if (biddRecords.biddRecordList.get(position).getDeal().equals("1")) {
			listItemView.toPay.setVisibility(View.VISIBLE);
			if (biddRecords.biddRecordList.get(position).getPayStatus().equals("0")) {
				 mContext.zhifuPosition= position;
				 listItemView.toPay.setText("未支付");
			}else {
				 listItemView.toPay.setText("已支付");
			}
			listItemView.toPay.setOnClickListener(onClickListener);
		}
		if (biddRecords.biddRecordList.get(position).getDeal().equals("1")) {
			listItemView.deal.setVisibility(View.VISIBLE);
		}else {
			listItemView.deal.setVisibility(View.GONE);
		}
		// if
		// (!biddRecords.biddRecordList.get(position).getImageUrl().equals(""))
		// {
		//
		// String url = "";
		// url = biddRecords.biddRecordList.get(position).getImageUrl();
		// listItemView.image.setTag(biddRecords.biddRecordList.get(position)
		// .getImageUrl());
		// if (mDownloader == null) {
		// mDownloader = new ImageDownloader();
		// }
		// if (mDownloader != null) {
		// mDownloader.imageDownload(url, listItemView.image,
		// "/watchtrade/image", activity, new OnImageDownload() {
		// @Override
		// public void onDownloadSucc(Bitmap bitmap,
		// String c_url, ImageView mimageView) {
		// ImageView imageView = (ImageView) listItemView.image
		// .findViewWithTag(c_url);
		// Drawable drawable = new BitmapDrawable(bitmap);
		// if (imageView != null) {
		// imageView.setBackgroundDrawable(drawable);
		// imageView.setTag("");
		// }
		// }
		// });
		// }
		// }

		return convertView;
	}

	private static final int REQUEST_CODE_PAYMENT = 1;

	public int getCount() {
		// TODO Auto-generated method stub
		return biddRecords.biddRecordList.size();
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub

		return 0;
	}

}